package com.authine.cloudpivot.web.api.secondopen.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * dingding工具包
 */
public class DingDingUtil {

    private static final Logger log = LoggerFactory.getLogger(DingDingUtil.class);

    /**
     * appkey
     * test dingirqt8ycl2gqizhsl
     * prod dingviqlpw4s1wbfmxpc
     */
    private final static String APPKEY="dingviqlpw4s1wbfmxpc";

    /**
     * appsecret
     * test ZK-qH8h7lrPpmCYtZgbd-CRxSQuu7p8alXz4eQc8YdExUOEEoUgISYDYbUgv0Yo2
     * prod 0z7tBoOI0J07iNOyTjGVwdsMtpGfpk3zXWifdPkq5yruaUq9kt7el4V0gsE9U5kW
     */
    private final static String APPSECRET="0z7tBoOI0J07iNOyTjGVwdsMtpGfpk3zXWifdPkq5yruaUq9kt7el4V0gsE9U5kW";

    /**
     * 请求头
     */
    private final static String URL="https://oapi.dingtalk.com";


    /**
     * 获取token
     * @return
     */
    public static   String getAccessToken() {
        String appkey = APPKEY;
        String appsecret = APPSECRET;
        DingTalkClient client = new DefaultDingTalkClient(URL+"/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appkey);
        request.setAppsecret(appsecret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            log.info("=====获取token失败=====");
            e.printStackTrace();
        }
        return response.getAccessToken();
    }

    /**
     * 获取用户信息
     * @return
     */
    public static OapiV2UserGetResponse getUserInfoById(String access_token, String userid){
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/user/get");
        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
        req.setUserid(userid);
        req.setLanguage("zh_CN");
        OapiV2UserGetResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====获取用户信息接口异常=====");
            e.printStackTrace();
        }
        return rsp;
    }

    /**
     * 根据手机号获取userid
     * @return
     */
    public static OapiV2UserGetbymobileResponse getUseridByMobile(String access_token, String mobile){
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/user/getbymobile");
        OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
        req.setMobile(mobile);
        OapiV2UserGetbymobileResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====根据手机号获取userid接口异常=====");
            e.printStackTrace();
        }
        return rsp;
    }

    /**
     * 创建用户
     */
    public static   void createUser(String access_token,String userid,String name,String mobile,String title,String email,String depid,String extension,Long inputtime){
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/user/create");
        OapiV2UserCreateRequest req = new OapiV2UserCreateRequest();
        //userid
        req.setUserid(userid);
        //姓名
        req.setName(name);
        //号码
        req.setMobile(mobile);
        //工号
        req.setJobNumber(userid);
        //职位
        req.setTitle(title);
        //邮箱
        req.setEmail(email);
        //所属部门id列表
        req.setDeptIdList(depid);
        //扩展字段
        req.setExtension(extension);
        //入职时间
        req.setHiredDate(inputtime);
        OapiV2UserCreateResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====创建用户接口异常=====");
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

    /**
     * 更新用户信息
     */
    public static void updateUser(String access_token,String userid,String name,String title,String email,String jobnumber,String parentid,String extension,Long rzsj) {
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/user/update");
        OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest();
        req.setUserid(userid);
        req.setName(name);
        req.setTitle(title);
        req.setEmail(email);
        req.setJobNumber(jobnumber);
        req.setDeptIdList(parentid);
        req.setExtension(extension);
        req.setHiredDate(rzsj);
        OapiV2UserUpdateResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====更新用户接口异常");
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

    /**
     * 更新用户信息，针对于部门
     */
    public  static void updateUser(String access_token,String userid,String depid){
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/user/update");
        OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest();
        req.setUserid(userid);
        req.setDeptIdList(depid);
        OapiV2UserUpdateResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====更新用户接口异常");
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

    /**
     * 删除用户
     */
    public static void deleteUser(String access_token,String ddid)  {
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/user/delete");
        OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest();
        req.setUserid(ddid);
        OapiV2UserDeleteResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            log.info("======删除用户接口异常=====");
            e.printStackTrace();
        }
    }

    /**
     * 根据部门id获取子部门id
     * 根目录为1
     */
    public static List<Long> getAllDepId(String access_token, Long depid) {
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/department/listsubid");
        OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
        //1为部门根节点，获取所有部门id
        req.setDeptId(depid);
        OapiV2DepartmentListsubidResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("获取部门ID接口异常");
            e.printStackTrace();
        }
        OapiV2DepartmentListsubidResponse.DeptListSubIdResponse result = rsp.getResult();
        //获取所有部门id列表
        List<Long> deptIdList = result.getDeptIdList();
        return  deptIdList;
    }

    /**
     * 获取部门下所有用户userid
     */
    public static List<String> getAllUserIdByDep(String access_token,Long depid){
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/user/listid");
        OapiUserListidRequest req = new OapiUserListidRequest();
        req.setDeptId(depid);
        OapiUserListidResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====获取部门下userid异常=====");
            e.printStackTrace();
        }
        List<String> useridList = rsp.getResult().getUseridList();
        System.out.println(rsp.getBody());
        return useridList;
    }

    /**
     * 创建部门
     */
    public static   Long createDep(String access_token,Long parentid,String depname){
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/department/create");
        OapiV2DepartmentCreateRequest req = new OapiV2DepartmentCreateRequest();
        //父部门id
        req.setParentId(parentid);
        //部门名称
        req.setName(depname);
        req.setOuterDept(true);
        req.setOuterDeptOnlySelf(true);
        OapiV2DepartmentCreateResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====创建部门接口异常=====");
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
        Long deptId = rsp.getResult().getDeptId();
        return deptId;
    }

    /**
     * 更新部门
     */
    public static void updateDep(String access_token,Long depid,String bmmc,Long parentId){
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/department/update");
        OapiV2DepartmentUpdateRequest req = new OapiV2DepartmentUpdateRequest();
        //部门id
        req.setDeptId(depid);
        req.setParentId(parentId);
        req.setName(bmmc);
        OapiV2DepartmentUpdateResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====更新部门接口异常=====");
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

    /**
     * 删除部门
     */
    public static   Long deleteDep(String access_token,Long depid) {
        DingTalkClient client = new DefaultDingTalkClient(URL+"/topapi/v2/department/delete");
        OapiV2DepartmentDeleteRequest req = new OapiV2DepartmentDeleteRequest();
        req.setDeptId(depid);
        OapiV2DepartmentDeleteResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            log.info("=====删除部门接口异常=====");
            e.printStackTrace();
        }
        return  rsp.getErrcode();
    }
}
